# -*- coding: UTF-8 -*-
import random
import os
import commands
import paramiko
def write_data(filename, data):
    fo = open(filename, 'a')
    fo.write('\n' + data)
    fo.close()



def read_data(filename):
    dl = []
    fo = open(filename, 'r')
    try:
        while True:
            line = fo.readline()
            if line:
                if 'distance' not in line:
                    ll = (line.replace("\n", '')).split(' ')
                    dl.append(ll)
            else:
                break
    except:
        print 'read error!!'
    fo.close()
    return dl


# 把数据中的0值变为1000（大数），并把数据标准化为int类型
# data为多维数组
def deal_data(data):
    new_data = []
    for dl in data:
        print '---',dl[0]
        if dl[0]=='':
            continue
        new_dl = []
        if len(dl)==0:
            continue
        for d in dl:
            if d == '0':
                new_dl.append(1000)
            else:
                new_dl.append(int(float(d)))
        new_data.append(new_dl)
    return new_data




# 判断列表中大于500的数有多少个
def check_list(dl):
    count = 0
    for d in dl:
        if d > 600:
            count = count + 1
    return count

# 以0(1000)值对数据进行分组
def split_data_by0(data):
    new_data = []
    l_group = []
    for dl in data:
        l_group.append(dl)
        if 1000 in dl:
            new_data.append(l_group)
            l_group = []
    return new_data

# merge some lists in one list
def merge_data(data):
    new_data = []
    for dl in data:
        new_group = []
        for d in dl:
            for n in d:
                new_group.append(n)
        new_data.append(new_group)
    return new_data


# random generate number list
# 50% in [27, 28]
# 30% in [20, 30]
# 20% in [14, 52]
def generate_random_sequence(nlength, p1=0.5, p2=0.3, p3=0.2):
    s = []
    n1 = int(nlength * p1)
    n2 = int(nlength * p2)
    n3 = int(nlength * p3)
    for i in range(0, n1):
        s.append(random.randint(27, 28))
    for i in range(0, n2):
        s.append(random.randint(20, 30))
    for i in range(0, n3):
        s.append(random.randint(14, 52))
    random.shuffle(s)
    return s


# list1 > list2, replace part of list1 of list2
def replace_l1_of_l2(l1, l2):
    n1 = random.randint(0, len(l1)-len(l2))
    j = 0
    for i in range(n1, n1 + len(l2)):
        l1[i] = l2[j]
        j = j + 1
    return l1

# pythonp
def ssh_scp_put(hostname, port, user, password,localFile, remoteFile):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, port=port, username=user, password=password)
    sftp = ssh.open_sftp()
    sftp.put(localFile, os.path.join(remoteFile, localFile.split('/')[-1]))
    sftp.close()
    ssh.close()


def collect_Data(cmd,fileNum, fileURL, count, regularData, fileType = 'normal',):
    '''
    :param cmd: 采集数据的命令
    :param fileType: 保存文件的类型，异常数据（被攻击的数据），正常数据
    :param fileNum: 采集数据保存文件的数量
    :param fileURL: 保存文件的路径
    :param count: 采集数据的数量
    :return:
    '''
    localFile = '/home/hh/desk/collectData/'
    #remoteFile = '/home/secsmarts/a/aa'
    remoteFile = '/Users/wuyou/Desktop/datafromUbuntu/'
    hostname = '172.20.10.9'
    port = 22
    user = 'wuyou'
    password = '     '
    #cmd = 'ping -c 10 www.baidu.com'
    for i in range(0,fileNum):
        # 生成文件名称,fileType为normal，则生成正常文件，fileType为abnormal 保存异常文件
        filename = ''
        if fileType == 'normal':
            filename = fileURL+'normalData/normal'+str(i)
            regularfile = regularData+"regularData/re_"
        elif fileType == 'abnormal':
            filename = fileURL+'abnormalData/11111111z'+str(i)
            regularfile = regularData + "abnoData/re_"
        if os.path.exists(filename):
            continue
        for count1 in range(0,count):
            return_code, output = commands.getstatusoutput(cmd)
            print count1
            ol = output.split('\n')
            ln_data = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
            i = 0
            for info in ol:
                if 'time' in info and 'bytes' in info:
                    t = info.split(' ')[7]
                    #print len(t)
                    nt = t[:len(t) - 2]
                    # print nt
                    # if float(nt) > 1000:
                    #     continue
                    ln_data[i] = str(nt)
                    i = i + 1
            wdata = ''
            for nd in ln_data:
                if wdata == '':
                    wdata = nd
                else:
                    wdata = wdata + ' ' + nd
            write_data(filename, wdata)
        regular_Data(filename,regularfile)
        localFile = regularfile + filename.split('/')[-1]
        print regularfile
       # ssh_scp_put(hostname=hostname, port=port, user=user, password=password, localFile=localFile, remoteFile=remoteFile)

        print 'Uploadfile success!!!:',regularfile
def regular_Data(OriginalData, regularData):
    '''
    :param OriginalData: 原始数据文件  均为数据文件路径
    :param regularData:  规则化后的数据文件：路径
    :return:
    '''

    regularfile = OriginalData
#filename = 'normal_data'
    data = read_data(OriginalData)
    data = deal_data(data)
    g_data = split_data_by0(data)
    m_data = merge_data(g_data)
    print OriginalData

    op_filename = regularData+OriginalData.split('/')[-1]
    for m in m_data:
        wdata = ''
        for d in m:
            d = str(d)
            if wdata == '':
                wdata = d
            else:
                wdata = wdata + ' ' + d
        print wdata
        write_data(op_filename, wdata)
"""
def regular_Data(OriginalData, regularData):
    '''
    :param OriginalData: 原始数据文件  均为数据文件路径
    :param regularData:  规则化后的数据文件：路径
    :return:
    '''
    for root, dirs, files in os.walk(OriginalData):
        print files
        #filename = files
        for file in files:
            # 苹果电脑里不知道那里来的.DS_Store文件。因此删除
            if file[0]=='.':
                files.remove(file)
               # print files
        for file in files:
            filename = OriginalData+file
        #filename = 'normal_data'
            data = read_data(filename)
            data = deal_data(data)
            g_data = split_data_by0(data)
            m_data = merge_data(g_data)

            op_filename = regularData+file
            for m in m_data:
                wdata = ''
                for d in m:
                    d = str(d)
                    if wdata == '':
                        wdata = d
                    else:
                        wdata = wdata + ' ' + d
                print wdata
                write_data(op_filename, wdata)
"""
if __name__ == '__main__':
    print '1111'
    url = '/home/hh/desk/collectData/'
    collect_Data(cmd='sudo l2ping -c 10 48:2C:A0:0F:1F:AD', fileNum=16, fileURL='/home/hh/desk/collectData/', count=1000, regularData='/home/hh/desk/regularData/', fileType='abnormal')
    regular_Data('/home/hh/desk/collectData/abnormalData/xiaomiscan1', '/home/hh/desk/regularData/regularData/')
    '''
    for root, dirs, files in os.walk(url):
        print len(files)
        if len(files)>=10:
            regular_Data('/home/hh/desk/collectData/', '/home/hh/desk/regularData/ab_')
    '''